﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace OCDP.Client.Resources.Converters
{
    /// <summary>
    /// this class aimes to convert datetime instance to string and vice versa
    /// http://www.silverlightshow.net/items/How-to-format-a-binding-value-using-convertors.aspx
    /// </summary>
    public class DateTimeConverter : System.Windows.Data.IValueConverter
    {
        CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-FR");

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return ((DateTime)value).ToString(culture);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            DateTime result;
            if (DateTime.TryParse(value.ToString(), culture, DateTimeStyles.AdjustToUniversal, out result))
                return result;
            return null;
        }
    }
}
